home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -serious- / programming / other / dopus412-gpl / library / dospaths.c < prev    next >
C/C++ Source or Header  |  2000-02-28  |  7KB  |  270 lines

  1. /*
  2.  
  3. Directory Opus 4
  4. Original GPL release version 4.12
  5. Copyright 1993-2000 Jonathan Potter
  6.  
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU General Public License
  9. as published by the Free Software Foundation; either version 2
  10. of the License, or (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. All users of Directory Opus 4 (including versions distributed
  22. under the GPL) are entitled to upgrade to the latest version of
  23. Directory Opus version 5 at a reduced price. Please see
  24. http://www.gpsoft.com.au for more information.
  25.  
  26. The release of Directory Opus 4 under the GPL in NO WAY affects
  27. the existing commercial status of Directory Opus 5.
  28.  
  29. */
  30.  
  31. #include "dopuslib.h"
  32.  
  33. struct PathList {
  34.     struct PathList *last;
  35.     char path[32];
  36. };
  37.  
  38. __asm __saveds DoAssign(register __a0 char *name,
  39.     register __a1 char *dir)
  40. {
  41.     struct RootNode *root;
  42.     struct DosInfo *dosinfo;
  43.     struct DeviceList *dl,*prevdev=NULL,*dev;
  44.     struct FileLock *flock;
  45.     BPTR lock;
  46.     unsigned char *str;
  47.     char buf[40],nname[40];
  48.     int len,found=ASSIGN_NODEV;
  49.  
  50.     strcpy(nname,name);
  51.     if ((len=strlen(nname))>0 && nname[len-1]==':') nname[len-1]=0;
  52.  
  53.     root=(struct RootNode *) DOSBase->dl_Root;
  54.     dosinfo=(struct DosInfo *)BADDR(root->rn_Info);
  55.     dev=(struct DeviceList *)BADDR(dosinfo->di_DevInfo);
  56.  
  57.     while (dev) {
  58.         BtoCStr(dev->dl_Name,buf,40);
  59.         if (LStrCmpI(buf,nname)==0) {
  60.             switch (dev->dl_Type) {
  61.                 case DLT_DIRECTORY:
  62.                     Forbid();
  63.                     if (prevdev==NULL) dosinfo->di_DevInfo=dev->dl_Next;
  64.                     else prevdev->dl_Next=dev->dl_Next;
  65.                     Permit();
  66.                     UnLock(dev->dl_Lock);
  67.                     str=(char *)(BADDR(dev->dl_Name));
  68.                     FreeMem(str,str[0]+1);
  69.                     FreeMem(dev,sizeof(struct DeviceList));
  70.                     dev=NULL; found=ASSIGN_OK;
  71.                     break;
  72.                 case DLT_DEVICE:
  73.                 case DLT_VOLUME:
  74.                     return(ASSIGN_CANCEL);
  75.             }
  76.         }
  77.         if (!dev) break;
  78.         prevdev=dev;
  79.         dev=(struct DeviceList *)BADDR(dev->dl_Next);
  80.     }
  81.  
  82.     if (!dir || !dir[0]) return(found);
  83.  
  84.     if (lock=Lock(dir,ACCESS_READ)) {
  85.         if (DOSBase->dl_lib.lib_Version>=36) {
  86.             if (AssignLock(nname,lock)) return(ASSIGN_OK);
  87.             return(ASSIGN_NODEV);
  88.         }
  89.         if (!(dl=(struct DeviceList *)AllocMem(sizeof(struct DeviceList),MEMF_CLEAR))) {
  90.             UnLock(lock);
  91.             return(ASSIGN_NOMEM);
  92.         }
  93.         if (!(str=AllocMem(len+1,MEMF_CLEAR))) {
  94.             FreeMem(dl,sizeof(struct DeviceList));
  95.             UnLock(lock);
  96.             return(ASSIGN_NOMEM);
  97.         }
  98.         CopyMem(nname,&str[1],len);
  99.         *str=len;
  100.         dl->dl_Name=(BSTR)(((long)str)>>2);
  101.         dl->dl_Lock=lock;
  102.         dl->dl_Type=DLT_DIRECTORY;
  103.         flock=(struct FileLock *)(BADDR(lock));
  104.         dl->dl_Task=flock->fl_Task;
  105.         Forbid();
  106.         dl->dl_Next=dosinfo->di_DevInfo;
  107.         dosinfo->di_DevInfo=(BPTR)(((long)dl)>>2);
  108.         Permit();
  109.         return(ASSIGN_OK);
  110.     }
  111.     return(ASSIGN_NODEV);
  112. }
  113.  
  114. char *__asm __saveds DoBaseName(register __a0 char *path)
  115. {
  116.     int a,b;
  117.  
  118.     if ((a=strlen(path)-1)<0) return(path);
  119.     if (path[a]=='/' || path[a]==':') if ((--a)<0) return(path);
  120.     for (b=a;b>=0;b--) {
  121.         if (path[b]=='/' || path[b]==':') return(&path[b+1]);
  122.     }
  123.     return(path);
  124. }
  125.  
  126. __asm __saveds DoPathName(register __a0 BPTR lock,
  127.     register __a1 char *buf,
  128.     register __d0 int len)
  129. {
  130.     struct PathList *list=NULL,*temp,*first=NULL;
  131.     struct FileInfoBlock __aligned finfo;
  132.     BPTR templock;
  133.     struct DOpusRemember *key=NULL;
  134.     int a,it;
  135.  
  136.     buf[0]=it=0;
  137.     do {
  138.         Examine(lock,&finfo);
  139.         if (!(temp=(struct PathList *)
  140.             DoAllocRemember(&key,sizeof(struct PathList),MEMF_CLEAR))) break;
  141.         if (!first) first=temp;
  142.         temp->last=list;
  143.         list=temp;
  144.         strcpy(list->path,finfo.fib_FileName);
  145.         templock=lock;
  146.         lock=ParentDir(lock);
  147.         if (it) UnLock(templock);
  148.         ++it;
  149.     } while (lock);
  150.     a=0;
  151.     while (list) {
  152.         DoStrConcat(buf,list->path,len);
  153.         if (a==0) DoStrConcat(buf,":",len);
  154.         else if (list->last) DoStrConcat(buf,"/",len);
  155.         list=list->last;
  156.         ++a;
  157.     }
  158.     DoFreeRemember(&key);
  159.     return((int)strlen(buf));
  160. }
  161.  
  162. __asm __saveds DoTackOn(register __a0 char *path,
  163.     register __a1 char *file,
  164.     register __d0 int len)
  165. {
  166.     int a;
  167.  
  168.     a=strlen(path)-1;
  169.     if (a>=0 && path[a]!=':' && path[a]!='/') DoStrConcat(path,"/",len);
  170.     if (file) DoStrConcat(path,file,len);
  171.     return((int)strlen(path));
  172. }
  173.  
  174. __asm __saveds DoCompareLock(register __a0 BPTR l1,
  175.     register __a1 BPTR l2)
  176. {
  177.     struct FileLock *lock1,*lock2;
  178.     struct DeviceList *dev1,*dev2;
  179.     char name1[40],name2[40];
  180.  
  181.     lock1=(struct FileLock *)BADDR(l1);
  182.     lock2=(struct FileLock *)BADDR(l2);
  183.  
  184.     if (lock1->fl_Task==lock2->fl_Task) {
  185.         dev1=(struct DeviceList *)BADDR(lock1->fl_Volume);
  186.         dev2=(struct DeviceList *)BADDR(lock2->fl_Volume);
  187.         BtoCStr(dev1->dl_Name,name1,40);
  188.         BtoCStr(dev2->dl_Name,name2,40);
  189.         if (LStrCmp(name1,name2)==0) {
  190.             if (lock1->fl_Key==lock2->fl_Key) return(LOCK_SAME);
  191.             return(LOCK_SAME_VOLUME);
  192.         }
  193.     }
  194.     return(LOCK_DIFFERENT);
  195. }
  196.  
  197. static char *pathlists[7]={
  198.     "Workbench","Initial CLI","Shell Process","New CLI",
  199.     "AmigaShell","New_WShell","Background CLI"};
  200.  
  201. __asm __saveds DoSearchPathList(register __a0 char *name,
  202.     register __a1 char *buf,
  203.     register __d0 int len)
  204. {
  205.     struct Process *proc,*myproc;
  206.     struct CommandLineInterface *cli;
  207.     BPTR *wext,lock,lock1;
  208.     APTR wsave;
  209.     int a,pass=0;
  210.  
  211.     myproc=(struct Process *)FindTask(0);
  212.     wsave=myproc->pr_WindowPtr;
  213.     myproc->pr_WindowPtr=(APTR)-1;
  214.  
  215.     if (lock=Lock(name,ACCESS_READ)) {
  216.         UnLock(lock);
  217.         myproc->pr_WindowPtr=wsave;
  218.         strcpy(buf,name);
  219.         return(1);
  220.     }
  221.     
  222.     Forbid();
  223.     proc=myproc;
  224.     for (a=0;a<8;a++) {
  225.         if (proc) {
  226.             if ((cli=(struct CommandLineInterface *)(proc->pr_CLI<<2))) {
  227.                 for (wext=(BPTR *)(cli->cli_CommandDir<<2);wext;wext=(BPTR *)(*wext<<2)) {
  228.                     if ((lock1=DupLock(wext[1]))) {
  229.                         PathName(lock1,buf,len);
  230.                         UnLock(lock1);
  231.                         TackOn(buf,name,len);
  232.                         if ((lock=Lock(buf,ACCESS_READ))) {
  233.                             pass=1;
  234.                             UnLock(lock);
  235.                             break;
  236.                         }
  237.                     }
  238.                 }
  239.             }
  240.         }
  241.         if (pass) break;
  242.         if (a<7) proc=(struct Process *)FindTask(pathlists[a]);
  243.     }
  244.     Permit();
  245.     myproc->pr_WindowPtr=wsave;
  246.     return(pass);
  247. }
  248.  
  249. __asm __saveds DoCheckExist(register __a0 char *name,
  250.     register __a1 int *size)
  251. {
  252.     int a=0;
  253.     BPTR mylock;
  254.     struct FileInfoBlock __aligned myfinfo;
  255.     struct Process *myproc;
  256.     APTR wsave;
  257.  
  258.     myproc=(struct Process *)FindTask(NULL);
  259.     wsave=myproc->pr_WindowPtr;
  260.     myproc->pr_WindowPtr=(APTR)-1;
  261.     if (mylock=Lock(name,ACCESS_READ)) {
  262.         Examine(mylock,&myfinfo);
  263.         UnLock(mylock);
  264.         a=myfinfo.fib_DirEntryType;
  265.         if (size) *size=myfinfo.fib_Size;
  266.     }
  267.     myproc->pr_WindowPtr=wsave;
  268.     return(a);
  269. }
  270.